package cn.zz.veggie.web.controller.mgmt;

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.core.types.result.PageResult;
import cn.zz.comm.types.IdNameObj;
import cn.zz.comm.web.annotation.BoxRespResult;
import cn.zz.veggie.application.service.ProductAppService;
import cn.zz.veggie.web.types.dto.mgmt.MgmtProductQueryDto;
import cn.zz.veggie.web.types.dto.mgmt.MgmtProductSaveDto;
import cn.zz.veggie.web.types.vo.mgmt.MgmtProductVo;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;

/**
 * 商品管理
 *
 * @author zhongq
 * @since 2024/4/4
 */
@Validated
@BoxRespResult
@RestController
@RequiredArgsConstructor
@RequestMapping("/mgmt/product")
public class MgmtProductController {

	private final ProductAppService productAppService;

	/**
	 * 商品列表
	 */
	@GetMapping("/list")
	public PageResult<MgmtProductVo> list(@Validated MgmtProductQueryDto query,
										  @RequestParam(defaultValue = "1") @Positive Integer current,
										  @RequestParam(defaultValue = "10") @Positive Integer size) {
		return productAppService.adminFindList(query, PageQuery.of(current, size));
	}

	/**
	 * 保存商品
	 */
	@PostMapping("/save")
	public void save(@RequestBody @Validated MgmtProductSaveDto dto) {
		productAppService.adminSave(dto);
	}

	/**
	 * 保存商品
	 */
	@DeleteMapping("/remove")
	public void removeProduct(@RequestParam Integer id) {
		productAppService.mgmtRemoveProduct(id);
	}

	/**
	 * 输入框搜索
	 *
	 */
	@GetMapping("/search")
	public List<IdNameObj<Integer>> inputSearch(String keyword) {
		return productAppService.adminInputSearch(keyword);
	}
}